package com.qsmx.qingqiuticket.app.delegate.back;

import android.os.Bundle;
import android.view.View;

import androidx.annotation.Nullable;

import com.qsmx.core.delegates.AtmosDelegate;
import com.qsmx.core.util.StringUtil;
import com.qsmx.core.util.storage.AtmosPreference;
import com.qsmx.qingqiuticket.app.R;
import com.qsmx.qingqiuticket.app.databinding.DelegateBackLoginTypeBinding;
import com.qsmx.qingqiuticket.app.event.RefreshSettingEvent;
import com.qsmx.qingqiuticket.app.fusion.FusionPre;

import org.greenrobot.eventbus.EventBus;

public class BackLoginTypeDelegate extends AtmosDelegate {

    private DelegateBackLoginTypeBinding mBinding;

    @Override
    public Object setLayout() {
        return R.layout.delegate_back_login_type;
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, View rootView) {

        mBinding = DelegateBackLoginTypeBinding.bind(rootView);
        if (AtmosPreference.getCustomStringPre(FusionPre.LOGIN_TYPE).equals("1") || StringUtil.isEmpty(AtmosPreference.getCustomStringPre(FusionPre.LOGIN_TYPE))) {
            mBinding.linMustLogin.setBackgroundResource(R.drawable.round_corner_back_edit_bg);
            mBinding.linUnMustLogin.setBackgroundResource(R.drawable.round_corner_white_bg);

            mBinding.tvMustLogin.setTextColor(requireContext().getResources().getColor(R.color.back_blue));
            mBinding.tvUnMustLogin.setTextColor(requireContext().getResources().getColor(R.color.gary_text));

            mBinding.cbMustLogin.setChecked(true);
            mBinding.cbUnMustLogin.setChecked(false);
        } else if (AtmosPreference.getCustomStringPre(FusionPre.LOGIN_TYPE).equals("2")) {
            mBinding.linMustLogin.setBackgroundResource(R.drawable.round_corner_white_bg);
            mBinding.linUnMustLogin.setBackgroundResource(R.drawable.round_corner_back_edit_bg);

            mBinding.tvMustLogin.setTextColor(requireContext().getResources().getColor(R.color.gary_text));
            mBinding.tvUnMustLogin.setTextColor(requireContext().getResources().getColor(R.color.back_blue));

            mBinding.cbMustLogin.setChecked(false);
            mBinding.cbUnMustLogin.setChecked(true);
        }
        onClick();
    }

    private void onClick() {
        mBinding.linMustLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mBinding.linMustLogin.setBackgroundResource(R.drawable.round_corner_back_edit_bg);
                mBinding.linUnMustLogin.setBackgroundResource(R.drawable.round_corner_white_bg);

                mBinding.tvMustLogin.setTextColor(requireContext().getResources().getColor(R.color.back_blue));
                mBinding.tvUnMustLogin.setTextColor(requireContext().getResources().getColor(R.color.gary_text));

                mBinding.cbMustLogin.setChecked(true);
                mBinding.cbUnMustLogin.setChecked(false);

                AtmosPreference.addCustomStringPre(FusionPre.LOGIN_TYPE, "1");

                EventBus.getDefault().post(new RefreshSettingEvent(new Bundle()));
            }
        });

        mBinding.linUnMustLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mBinding.linUnMustLogin.setBackgroundResource(R.drawable.round_corner_back_edit_bg);
                mBinding.linMustLogin.setBackgroundResource(R.drawable.round_corner_white_bg);

                mBinding.tvUnMustLogin.setTextColor(requireContext().getResources().getColor(R.color.back_blue));
                mBinding.tvMustLogin.setTextColor(requireContext().getResources().getColor(R.color.gary_text));

                mBinding.cbUnMustLogin.setChecked(true);
                mBinding.cbMustLogin.setChecked(false);

                AtmosPreference.addCustomStringPre(FusionPre.LOGIN_TYPE, "2");
                EventBus.getDefault().post(new RefreshSettingEvent(new Bundle()));
            }
        });

        mBinding.tvBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getSupportDelegate().pop();
            }
        });
    }
}
